package leetcode_600;

/**
 *@author 周杨
 *FractionAdditionAndSubtraction_592 解决分数加减法的问题
 *describe:AC 54% 字符串匹配
 *2018年9月17日 下午1:19:16
 */
public class FractionAdditionAndSubtraction_592 {
	/**
	 * describe:寻找最大公约数
	 * 2018年9月17日 下午1:18:47
	 */
	public int findGCD(int a, int b) {
        return a!=0? findGCD(b%a, a): b;
    }
    
    public String fractionAddition(String expression) {
        String fracs[] = expression.split("(?=[+-])");
        int resNumerator = 0, resDenominator = 1;
        for(String frac: fracs) {
            String fracSplitted[] = frac.split("/");
            int num = Integer.parseInt(fracSplitted[0]);
            int denom = Integer.parseInt(fracSplitted[1]);
            resNumerator = resNumerator*denom + num*resDenominator;
            resDenominator = resDenominator * denom;
            
            int gcd = findGCD(resNumerator, resDenominator);
            resNumerator /= gcd;
            resDenominator /= gcd;
        }
        String sign = resNumerator<0 || resDenominator<0? "-" : "";
        
        return sign + Math.abs(resNumerator) + "/" + Math.abs(resDenominator);
    }
}
